﻿using AutoMapper;
using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.ReadApi.Command;
using U.B.ReadApi.Dto;
using U.B.Repistory;

namespace U.B.ReadApi.CommandHander
{
    public class GetNavBarCommandHander : IRequestHandler<GetNavBarCommand, ApiResult<List<NavBarDTO>>>
    {
        private readonly IRepistory<Menu> menuRep;
        private readonly IMapper mapper;
public GetNavBarCommandHander(IRepistory<Menu> menuRep, IMapper mapper)
        {
            this.menuRep = menuRep;
            this.mapper = mapper;
        }
        public Task<ApiResult<List<NavBarDTO>>> Handle(GetNavBarCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<NavBarDTO>> result = new ApiResult<List<NavBarDTO>>();
            result.code =Renum.显示成功;
            result.Message = "显示成功";
            result.Data = GetTree(0);
            return Task.FromResult(result);
        }
        public List<NavBarDTO> GetTree(int pid)
        {
            var m = menuRep.GetAll().Where(x => x.PId == pid).ToList();
            var a = new List<NavBarDTO>();
            foreach (var item in m)
            {
                var mm = new NavBarDTO(item.MenuId, item.MenuName, item.MenusUrl, GetTree(item.MenuId));
                a.Add(mm);
            }
            return a;

        }
    }
}
